import java.util.*;

/**
 * @author LKQ
 * @date 2022/6/6 22:37
 * @description 滑动窗口
 */
public class Solution2 {
    public static void main(String[] args) {
        Solution2 solution2 = new Solution2();
        solution2.maxFreq("aababcaab", 2, 3, 4);
    }
    public int maxFreq(String s, int maxLetters, int minSize, int maxSize) {

        Map<String, Integer> map = new HashMap<>();
        int ans = 0;

        for (int len = minSize; len <= maxSize; len++) {
            int[] cnt = new int[26];
            for (int i = 0; i < len; i++) {
                cnt[s.charAt(i) - 'a']++;
            }
            for (int i = 0; i + len <= s.length(); i++) {
                if (check(cnt, maxLetters)) {
                    String t = s.substring(i, i + len);
                    map.put(t, map.getOrDefault(t, 0) + 1);
                    if (map.get(t) > ans) {
                        ans = map.get(t);
                    }
                }
                if (i + len == s.length()) {
                    break;
                }
                cnt[s.charAt(i) - 'a']--;
                cnt[s.charAt(i + len) - 'a']++;
            }
        }
        return ans;
    }
    public boolean check(int[] cnt, int maxLetters) {
        for (int i = 0; i < 26; i++) {
            if (cnt[i] != 0) {
                maxLetters--;
                if (maxLetters < 0) {
                    return false;
                }
            }
        }
        return true;
    }
}
